package lnu.project.cart.content;

import java.util.HashMap;

import lnu.project.cart.entity.Product;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

public class ProductContentProvider extends ContentProvider {
	public static final String DB_PRODUCTS = "lnuShoppingDB.db";
	
	private static final Uri CONTENT_URI = Uri.parse(
			"content://com.lnu.project.cart/product");
	public static final int URI_CODE = 1;
    public static final int URI_CODE_ID = 2;

    private static final UriMatcher mUriMatcher;
    private static HashMap<String, String> mContactMap;
	    
    private SQLiteDatabase db;
    
    static {
    	
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        
        mUriMatcher.addURI("com.lnu.project.cart", 
        		ProductDatabaseHelper.DATABASE_TABLE, URI_CODE);
        
        mUriMatcher.addURI("com.lnu.project.cart", 
        		ProductDatabaseHelper.DATABASE_TABLE + "/#", URI_CODE_ID);

        mContactMap = new HashMap<String, String>();
        mContactMap.put(Product.ID, Product.ID);
        mContactMap.put(Product.BASKET_ID, Product.BASKET_ID);
        mContactMap.put(Product.TYPE_OF_PRODUCT, Product.TYPE_OF_PRODUCT);
        mContactMap.put(Product.NAME, Product.NAME);
        mContactMap.put(Product.BARCODE, Product.BARCODE);
        mContactMap.put(Product.QUANTITY, Product.QUANTITY);
        mContactMap.put(Product.WEIGHT, Product.WEIGHT);
        mContactMap.put(Product.PRICE, Product.PRICE);
        
    }
    public static Uri getUri(){
    	return CONTENT_URI;
    }
    
    public String getDbName() {
        return(DB_PRODUCTS);
    }
    
	@Override
	public int delete(Uri url, String where, String[] whereArgs) {
		int retVal = db.delete(ProductDatabaseHelper.DATABASE_TABLE, where, whereArgs);
        getContext().getContentResolver().notifyChange(url, null);
        return retVal;
	}

	@Override
	public String getType(Uri arg0) {
		return null;
	}

	@Override
	public Uri insert(Uri url, ContentValues inValues) {
		System.out.println("insert product");
		ContentValues values = new ContentValues(inValues);
System.out.println("Insert product");
        long rowId = db.insert(ProductDatabaseHelper.DATABASE_TABLE, null, values);
        if (rowId > 0) {
            Uri uri = ContentUris.withAppendedId(CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(uri, null);
            return uri;
        }
        else {
        	throw new SQLException("Failed to insert row into " + url);
        }
	}

	@Override
	public boolean onCreate() {
		System.out.println("Create Product Provider");
		db = (new ProductDatabaseHelper(getContext())).getWritableDatabase();
        return (db == null) ? false : true;
	}

	@Override
	public Cursor query(Uri url, String[] projection, 
            String selection, String[] selectionArgs, String sort) {
		String orderBy;       
        if (TextUtils.isEmpty(sort)) {
            orderBy = Product.NAME;
        } 
        else {
            orderBy = sort;
        }

        Cursor c = db.query(ProductDatabaseHelper.DATABASE_TABLE, projection, selection, selectionArgs, 
                null, null, orderBy);
        c.setNotificationUri(getContext().getContentResolver(), url);
        return c;
	}

	@Override
	public int update(Uri url, ContentValues values, 
            String where, String[] whereArgs) {
		int retVal = db.update(ProductDatabaseHelper.DATABASE_TABLE, values, where, whereArgs);
	    
        getContext().getContentResolver().notifyChange(url, null);
        return retVal;
	}
}
